*==========================================================================================================================================================
*
*	Local Projections
*
*==========================================================================================================================================================


set more off

qui {


*-------------------------------------------------------------------------------------------------------------
*	SETTINGS
*-------------------------------------------------------------------------------------------------------------

* Variables
local vars "gdp_r_adj cpi_sil log_R_esp_er_hp itsN wage_sil " 
local lava ""Real output" "Consumer prices" "Lending rate" "Money inflow (% of stock)" "Nominal wage""
local yti ""Percent" "Percent" "Percentage pts" "Percentage pts" "Percent""

* irf horizon
local H 5
local F 5

* lags
local L 2
local LS `L'
local P 100

local drops = 3

*-------------------------------------------------------------------------------------------------------------

local count = 0
foreach v of local vars {
local count = `count'+1
n display "variable `v'"

*_________ GET DATA ____________
use "`path'data/working files/prepared.dta", clear /*read in data*/


*____ DECLARE TIME SERIES _____
gen id =1
tsset id year, yearly


gen period = 1
local jj = 1
forvalues y = 0(100)300 {
local jj = `jj'+1
	replace period = `jj' if year >= 1500 + `y' 
	}
tab period, gen(ppp)
order year period



*____ GENERATE RANDOM RESHUFFLINGS OF PERCLOSS _____
set seed 1

forvalues p = 0/`P' { //subset runs
n display "variable `v' - run `p'"

*_________________ LOCAL PROJECTIONS _________________

***_________ CONTROLS

local indep	" gdp_r_adj_gr cpi_sil_gr wage_sil_gr itsN_gr" 

forvalues i=0/`L'{
	local indep_`i' = subinstr("`indep'"," "," l`i'_",.)
	}
	
local rhs ""
forvalues i = 1/`L'{
	local rhs "`rhs'`indep_`i''"
	}
	
	local not l0_`v'_gr l0_itsN_gr
	local rhs: list rhs- not
	
	

	***_________ IMPULSE RESPONSE variables
	if (`p'==0) {
		gen irf_`v' = . if _n<=`H'+1
		}
	else {
		gen irf_`v'_`p' = . if _n<=`H'+1
		}
		

	***_________ ESTIMATE COEFFICIENTS
	
	forvalues y = 0/`H' {
		n display "projection `y'"

			
		if (`p'==0) {
			reg `v'_gr`y' l(1/`LS').`v'_gr l(-`F'/`LS').perclossN_vellon l(-`F'/`LS').perclossN `rhs' l(-`F'/`LS').percloss_salvN l(-`F'/`LS').percloss_pirN l(0/`LS').temperature l(0/`LS').warbeu l(0/`LS').warbciv, vce(robust)  	
			}
			
		else {
		
			// select disaster list
			preserve
			
			forvalues s=1/`drops' { //number of diasters to drop
			gen cumdis = sum(disaster)
			sum cumdis
			generate select = floor((`r(max)'-1+1)*runiform() + 1) if _n<=1
			sum select if _n==1

			replace disaster = . if cumdis == `r(mean)' & d.cumdis==1 
			replace perclossN = . if cumdis == `r(mean)' & d.cumdis==1 
			
			order year disaster cumdis select perclossN
			drop cumdis select
			tsset id year
			}
	
			reg `v'_gr`y' l(1/`LS').`v'_gr l(-`F'/`LS').perclossN_vellon l(-`F'/`LS').perclossN `rhs' l(-`F'/`LS').percloss_salvN l(-`F'/`LS').percloss_pirN l(0/`LS').temperature l(0/`LS').warbeu l(0/`LS').warbciv, vce(robust) 
				
			restore
			}
		

		* Shock response
		if (`p'==0) {
			lincom perclossN 
			replace irf_`v' = r(estimate) if _n==`y'+1 /* coefficients */
			}
		else {
			lincom perclossN 
			replace irf_`v'_`p' = r(estimate) if _n==`y'+1 /* coefficients */
			}
		}			


	} //subset runs
	

// select percentiles
egen ub_`v' = rowpctile(irf_`v'_*), p(15.9)
egen lb_`v' = rowpctile(irf_`v'_*), p(84.1)
egen ub99_`v' = rowpctile(irf_`v'_*), p(99)
egen lb99_`v' = rowpctile(irf_`v'_*), p(1)
egen max_`v' = rowmax(irf_`v'_*)
egen min_`v' = rowmin(irf_`v'_*)


	
* plot prerequisites
gen time = _n-1 if(_n<=`H'+1) 
drop if time > `H'
drop if time == .
gen zero = 0 if time!=.

local title : word `count' of `lava'
 local ytitle : word `count' of `yti'
local lsize "medlarge"
local tsize "medlarge"

twoway  (line lb99_`v' time, lcolor(black) lpattern(dash) lwidth(medthick)) ///
		(line ub99_`v' time, lcolor(black) lpattern(dash) lwidth(medthick)) ///
		(line irf_`v' time, lcolor(black) lpattern(solid) lwidth(medthick)) ///
		(line zero time, lcolor(black) lwidth(medium)), title("") xtitle("Year", size(`lsize')) ytitle(`ytitle', height(2) size(`lsize')) xlabel(,labsize(`lsize')) ylabel(, format(%9.1f) nogrid angle(0) labsize(`lsize')) ///
		legend(off) title(`title', size(`tsize')) ///
		graphregion(color(white)) plotregion(color(white)) name(g`count', replace) nodraw



} // vars

graph combine g4 g1 g2 g5 g3, name(LP_subset_presi, replace) cols(3) imargin(medsmall) plotregion(color(white)) graphregion(color(white)) 
graph display LP_subset_presi, ysize(4) xsize(6)
graph export "results/FigureA1.pdf", replace 	

}


*_________________________________________

			
